--
-- Author: yawei.zhu
-- Date: 2018-09-26 15:14:09
--
-- WorldBossController
--

local WorldBossView = import("..views.WorldBossView")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ControllerBase)

function ClassRef:ctor(data)
    self.super.ctor(self, data)
end

function ClassRef:init()
    self._model = app:getInst("BossModel")
end

function ClassRef:removeListeners()
    self.super.removeListeners(self)
end

function ClassRef:onEvent(eventName, data)
end

function ClassRef:listEvent()
    return {}
end


function ClassRef:openView(data)
    self._entrance = 1
    if data then
        self._entrance = data
    end
    self._model:reqWorldBossInfo()
end

function ClassRef:showView(bossInfo)
    if not bossInfo then return end
    --dump(bossInfo)

    if cc.isDead(self._view) then 
        self._view = WorldBossView.new(app.mvc.VIEWTYPE_BLUR)
        self._view:onCloseView(handler(self, self.closeView))
        self._view:onClearView(handler(self, self.clearView))
        self._view:onOperate(handler(self, self.onOperate))
        self:addChild(self._view, data)
    end
    
    self._view:show()
    self._view:updateView(self._entrance, bossInfo)
end

function ClassRef:closeView(data)
    if self._view then
        self:removeChild(self._view, data)
        self:clearView()
    end
end
function ClassRef:clearView()
    if self._view then
        self._view = nil
    end
end

function ClassRef:onUpdateWorldBossInfo(data)
    if not cc.isDead(self._view) then 
        self._view:updateViewBox(data)
    end
end

-------------------------------------------------------
function ClassRef:trackWorldBoss()
    local npcId = GD:getWorldBossNpcId(app:getWeekDay(), MapManager:getMapId())
    if npcId == nil then -- 如果当前地图没有世界boss，就去追踪102地图的boss
        npcId = GD:getWorldBossNpcId(app:getWeekDay(), 102)
    end
    if npcId then
        EM:notify("onTrackingWay",{trackNpcId=tonumber(npcId), autoStartEvents=false, isTracking=true,immediately = true})
        FieldManager:setTrackData(Enums.trackType.worldBoss)
    end
end

-- 展示战备UI
function ClassRef:shownReadyUIOrTeamSettingUI()
    local params = {
        idx = 1,
        targetId = Enums.TeamTarget.worldBoss
    }

    local teamNum = app:getInst("TeamModel"):getMemberNum()
    if teamNum < 5 then
        local function teamMemberEnsureDlg()
            local createTeamDialog = display.newConfirmDialog(L("startChallengeConfirm"), function() -- 确定
					--self:onOperate("TeamMatching_changeAutoRecruitTarget")-- 切换自动招募目标
					app:sendMsg("NewTeamReadyAndSettingController", "openView", params)
				end)
			UIHandler:addPopDialog( createTeamDialog )
        end
        teamMemberEnsureDlg()
    else
        -- self:onOperate("TeamMatching_changeAutoRecruitTarget")-- 切换自动招募目标
        app:sendMsg("NewTeamReadyAndSettingController", "openView", params)
    end
end

-------------------------------------------------------
function ClassRef:onOperate(op, data)
    print("WorldBossController:onOperate op = " .. op)
    if op == "openWorldBossTeam" then
        app:sendMsg("TeamMatchController", "openView", {targetId=Enums.TeamTarget.worldBoss})
    elseif op == "reqCurRank" then
        app:sendMsg("BossRankController", "openView", data - 1)
    elseif op == "onTrackWorldBoss" then
        self:trackWorldBoss()
    elseif op == "shownReadyUIOrTeamSettingUI" then
        self:shownReadyUIOrTeamSettingUI()
    end
end

return ClassRef
